﻿using System;
using System.Reflection;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using log4net;
namespace AnyOffice.general.netdisk
{
    public partial class newfile : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                string str = (Request.QueryString["parent_folder"] != null) ? Request.QueryString["parent_folder"].ToString() : "/";
                this.ViewState["ParentFolder"] = str;
                NetdiskInfo netdiskInfo = NetdiskFactory.GetNetdiskInfo();
                if (netdiskInfo == null)
                {
                    Response.Redirect("index.aspx");
                }
                else if (!netdiskInfo.CanNew && !netdiskInfo.CanManage)
                {
                    Response.Redirect("index.aspx");
                }
                else
                {
                    this.litNavTitle.Text = netdiskInfo.Name;
                    this.parentPath.DataSource = new LiteUtility().GetDtbDirectory();
                    this.parentPath.DataTextField = "path";
                    this.parentPath.DataValueField = "path";
                    this.parentPath.DataBind();
                    if (this.parentPath.Items.FindByValue(str) != null)
                    {
                        this.parentPath.Items.FindByValue(str).Selected = true;
                    }
                    this.btnSubmit.Attributes.Add("onclick", "return CheckForm();");
                }
            }
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("netdisk.aspx?path=" + Server.UrlEncode(this.ViewState["ParentFolder"].ToString()));
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.pnlMain.Visible = false;
            string str = (NetdiskFactory.GetNetdiskInfo().DiskPath + this.parentPath.Value).Replace("/", @"\");
            if (!str.EndsWith(@"\"))
            {
                str = str + @"\";
            }
            try
            {
                Upload upload = new Upload();
                upload.AutoCreateDirectory = true;
                upload.OverwriteFile = true;
                upload.DestinationPath = str;
                if (this.File1.PostedFile.ContentLength > 0)
                {
                    upload.GetFile(this.File1.PostedFile);
                    upload.DoUpload();
                }
                if (this.File2.PostedFile.ContentLength > 0)
                {
                    upload.GetFile(this.File2.PostedFile);
                    upload.DoUpload();
                }
                if (this.File3.PostedFile.ContentLength > 0)
                {
                    upload.GetFile(this.File3.PostedFile);
                    upload.DoUpload();
                }
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception);
                }
                this.litMessage.Text = base.ShowPrompt("上传文件失败！", "netdisk.aspx?path=" + Server.UrlEncode(this.ViewState["ParentFolder"].ToString()));
                return;
            }
            Response.Redirect("netdisk.aspx?path=" + Server.UrlEncode(this.ViewState["ParentFolder"].ToString()));
        }


    }
}
